package com.ithao.service.impl;

import com.ithao.dao.CatalogDao;
import com.ithao.database.MmDaoException;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.pojo.Catalog;
import com.ithao.service.CatalogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description ：学科目录业务实现类
 * @version: 1.0
 */
@Service
@Slf4j
@Transactional
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CatalogDao catalogDao;

    @Override
    public void addCatalog(Catalog catalog) {
        log.info("addCataglog:{}", catalog);
        Integer result = catalogDao.addCatalog(catalog);
        if (result == 0) {
            throw new MmDaoException("删除学科目录失败");
        }
    }

    @Override
    public PageResult findListByPage(QueryPageBean queryPageBean) {
        // 获取数据集
        List<Catalog> catalogList = catalogDao.selectListByPage(queryPageBean);
        // 获取总记录数
        Long totalCount = catalogDao.selectTotalCount(queryPageBean);
        return new PageResult(totalCount, catalogList);
    }

    @Override
    public void deleteCatalog(Integer id) {
        log.info("deleteCatalog:id:{}", id);
        Integer result = catalogDao.deleteCatalog(id);
        if (result == 0) {
            throw new MmDaoException("删除学科目录失败");
        }
    }

    //目录的启用禁用
    @Override
    public void updateStatus(Integer catalogId, Integer status) {
        int result = catalogDao.updateStatus(catalogId, status);
        if (result == 0) {
            throw new MmDaoException("更改目录状态失败");
        }
    }

    @Override
    public void update(Catalog catalog) {
        catalogDao.update(catalog);
    }

}
